<template>
  <div>
    <div>
      <el-button type="primary" @click="addItemVisible = true">添加</el-button>
      <br>
    </div>
    <el-dialog append-to-body :visible.sync="addItemVisible">
      <el-form :model="addItemData">

        <el-form-item label="字典key">
          <el-input v-model="addItemData.dictKey"/>
        </el-form-item>
        <el-form-item label="字典值">
          <el-input v-model="addItemData.dictValue"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="createItem">确定</el-button>
        </el-form-item>
      </el-form>


    </el-dialog>


    <el-table v-loading="tableLoading" :data="dictItemData" border>
      <el-table-column label="字典key" prop="dictKey"></el-table-column>
      <el-table-column label="字典翻译" prop="dictValue"></el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-popconfirm
            confirm-button-text='好的'
            cancel-button-text='不用了'
            icon="el-icon-info"
            icon-color="red"
            @confirm="removeDictItem(scope.row)"
            title="确定删除吗？"
          >
            <el-button size="small" slot="reference"  type="danger">删除</el-button>
          </el-popconfirm>
        </template>

      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {getDictItem,removeDictItem,createDictItem} from "@/api/system/dictItem";

export default {
  name: "DictItem",
  props:['dictId'],
  data(){
    return{
      dictItemData:[],
      addItemVisible:false,
      addItemData:{
        dictKey:'',
        dictValue:''
      },
      tableLoading:false,
    }
  },
  mounted() {
    this.loadDictItemList();
  },
  methods:{
    createItem(){
      createDictItem({...this.addItemData,dictId:this.dictId}).then(res =>{
        this.$message({
          message:res.message,
          type:'info'
        })
        if(this.$isSuccess(res)){
          this.addItemVisible = false;
          this.loadDictItemList();
        }
      })
    },
    loadDictItemList(){
      this.tableLoading = false;
      getDictItem(this.$props.dictId).then(res =>{
        this.dictItemData = res.data;
        this.tableLoading = false;
      })
    },
    removeDictItem(item){
      removeDictItem(item.itemId).then(res =>{
        this.$message({
          message:res.message,
          type:'info'
        })
        this.loadDictItemList();

      })
    },
  },
}
</script>

<style scoped>

</style>
